#include "DXUT.h"
#include "Player.h"
#include "../World.h"

Player::Player(const D3DXVECTOR3& pos) : EntityLiving(pos, D3DXAABB(D3DXVECTOR3(-0.4f, -0.0f, -0.4f), D3DXVECTOR3( 0.4f,  1.8f,  0.4f))) {
	m_View			= D3DXVECTOR3(0.0f, 0.0f, 0.0f);

	m_Exhaused		= false;
	m_Juced			= false;
	m_JucedTimeLeft = 0.0f;
}

Block Player::getSelectedItem() {
	return m_ActionbarItems[m_SelectedItem];
}

D3DXVECTOR3	Player::getPosition() {
	return m_Position;
}

void Player::setPosition(D3DXVECTOR3 position) {
	m_Position = position;
}

D3DXVECTOR3	Player::getEyePoint() {
	D3DXVECTOR3 Eye = D3DXVECTOR3(m_Position); Eye.y += 1.8f;
	return Eye;
}

D3DXVECTOR3	Player::getViewDirection() {
	return m_View;
}

void Player::OnFrameMove( EntityWorld * pWorld, double fTime, float fElapsedTime) {
	EntityLiving::OnFrameMove(pWorld, fTime, fElapsedTime);

}

void Player::setJuced(float fTime) {
	m_JucedTimeLeft = max(m_JucedTimeLeft, fTime);
	if (m_JucedTimeLeft > 0.0f) {
		m_Juced = true;
	}
};

void Player::calcStatus(float fElapsedTime) {
	if (m_Juced) {
		m_EPreg	= 10.0f;
		m_JucedTimeLeft -= fElapsedTime;
		if (m_JucedTimeLeft <= 0.0f) {
			m_Juced			= false;
			m_JucedTimeLeft = 0.0f;
		}
	} else if (m_Exhaused) {
		m_EPreg	= 5.0f;
	} else {
		if (m_Chilled.aktiv())
			m_EPreg	= 5.0f;
		else
			m_EPreg	= 10.0f;
	}

	if (m_Burning.aktiv()) {
		inflictDamage(Damage(FIRE, fElapsedTime * 5.0f));
		m_Burning -= fElapsedTime;
	}

	if (m_Poisoned.aktiv()) {
		inflictDamage(Damage(NATURE, fElapsedTime * 1.0f));
		m_Poisoned -= fElapsedTime;
		if (!m_Poisoned.aktiv()) {
			m_HPregmod = 1.0f;
		} else {
			m_HPregmod = 0.5f;
		}
	}

	if (m_Chilled.aktiv()) {
		m_Chilled -= fElapsedTime;
	}

	if (m_Regeneration.aktiv()) {
		m_Regeneration -= fElapsedTime;
		if (m_HP < m_MaxHP) {
			m_HP += fElapsedTime * 2.0f * m_Regeneration.iStrength;
			if (m_HPreg > 0.0f) {
				m_HPreg -= min(fElapsedTime * 2.0f * m_Regeneration.iStrength, m_HPreg);
				if (m_HP + m_HPreg > m_MaxHP)
					m_HPreg = m_MaxHP - m_HP;
			}
			if (m_HP > m_MaxHP)
				m_HP = m_MaxHP;
		}
	}
};

void Player::Movement(D3DXVECTOR3 rotation, bool alt, bool j, bool f, bool b, bool l, bool r, float fElapsedTime) {
	m_View = rotation;
	if (m_Exhaused && !m_Juced) {
		if (m_EP > 25.0) {
			m_Exhaused = false;
		}
	} else {
		if (j) {
			if (m_Swimming) {
				m_Velocity.y += 5.0f;
				m_Velocity.y = min(m_Velocity.y, 5.0f);
			} else if (m_Grounded) {
				m_Velocity.y += 10.0f;
				m_Velocity.y = min(m_Velocity.y, 15.0f);

				if (!m_Juced) {
					if (alt && f)
						m_EP = max(0.0, m_EP - 3.0);
					else
						m_EP = max(0.0, m_EP - 5.0);
					if (m_EP <= 0.0) {
						m_Exhaused = true;
					}
				}
			}
		}
		if (alt && (f || b || l || r)) {
			if (!m_Juced) {
				m_EP = min(m_MaxEP, m_EP - fElapsedTime * 15.0f);
				if (m_EP <= 0.0) {
					m_Exhaused = true;
				}
			}
		}
	}

	if (!m_Swimming) {
		rotation.y = 0.0f;
		D3DXVec3Normalize(&rotation, &rotation);
	}		

	D3DXVECTOR3 dirNorm = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
	if (f) {
		D3DXVECTOR3 dir = rotation;
		dirNorm += dir;
	}
	if (b) {
		D3DXVECTOR3 dir = rotation;
		dirNorm -= dir;
	}
	if (l) {
		D3DXVECTOR3 dir =  rotation;
		D3DXMATRIX mRot;
		D3DXMatrixRotationY(&mRot, -D3DX_PI / 2);
		D3DXVec3TransformCoord(&dir, &dir, &mRot);

		dirNorm += dir;
	}
	if (r) {
		D3DXVECTOR3 dir = rotation;
		D3DXMATRIX mRot;
		D3DXMatrixRotationY(&mRot, D3DX_PI / 2);
		D3DXVec3TransformCoord(&dir, &dir, &mRot);

		dirNorm += dir;
	}
	if (D3DXVec3Length(&dirNorm) > 0.01f) {
		D3DXVec3Normalize(&dirNorm, &dirNorm);
		float speed = 120.0f;
		if (m_Exhaused) 
			speed = 50.0f;
		else if (alt)
			speed = 250.0f;	
		if (m_Chilled.aktiv()) speed *= 0.7f;
		if (m_Juced)   speed *= 1.2f;
		D3DXVec3Scale(&dirNorm, &dirNorm, fElapsedTime * speed);
		m_Velocity += dirNorm;
	}
}